<?php

function cc_admin_theme() {
  $hooks = array();
  $hooks['cc_user_admin_permissions'] = array(
    'render element' => 'form',
  );
  return $hooks;
}

/**
 * Implements hook_html_head_alter().
 * We are overwriting the default meta character type tag with HTML5 version.
 */
function cc_admin_html_head_alter(&$head_elements) {
  $head_elements['system_meta_content_type']['#attributes'] = array(
    'charset' => 'utf-8'
  );

  $head_elements['ie_edge'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'http-equiv' => 'X-UA-Compatible',
      'content' => 'IE=edge',
    ),
  );

  $head_elements['webkit_renderer'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'renderer',
      'content' => 'webkit',
    ),
  );
}

function cc_admin_preprocess_html(&$variables) {
  drupal_add_css(path_to_theme().'/cc_admin.css', array('group'=> CSS_THEME, 'weight'=> 999));
  drupal_add_css(path_to_theme().'/custom.css', array('group'=> CSS_THEME, 'weight'=> 999));

  drupal_add_js(path_to_theme().'/cc_admin.js');

}

function cc_admin_preprocess_page(&$variables) {
  global $user;


  $variables['title_prefix']['logo'] = array(
    '#markup' => theme('image', array('path' =>$variables['logo'], 'attributes' => array('class'=> 'logo'))),
  );
  $variables['title_prefix']['site_name'] = array(
    '#markup' => '<h1 id="site-name">'.$variables['site_name'] .'</h1>',
  );

  if ($variables['logged_in']) {
    $variables['screen_name'] = $screen_name = format_username($user);
    $variables['title_suffix']['user'] = array(
      '#markup' => '<div class="user-links"><span>您好, '. $screen_name . '</span><a class="logout" href="'.url('user/logout').'">登出</a></div>',
    );

    drupal_set_breadcrumb(array(drupal_get_title()));
  }
  else {
    drupal_set_breadcrumb(array());
    unset($variables['page']['sidebar_left']);
  }

}

function cc_admin_process_page(&$variables) {
  // $breadcrumb = $variables['breadcrumb'];
  // array_unshift($variables['page']['content'], array('#markup' => $breadcrumb));
  // $variables['breadcrumb'] = false;
  // $variables['primary_local_tasks'] = false;
  // $variables['secondary_local_tasks'] = false;
}

//menu_link__menu_site_admin_menu
function cc_admin_menu_link($variables) { //dpm($variables);
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {

    if (isset($_GET['destination'])) {

      foreach (element_children($element['#below']) as $key) {
        if ($element['#below'][$key]['#href'] == $_GET['destination']) {
          $element['#attributes']['class'][] = 'active-trail';
          // $element['#below'][$key]['#localized_options']['attributes']['class'][] = 'active-trail active';
          break;
        }
      }
    }

    $sub_menu = drupal_render($element['#below']);
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

function cc_admin_form_taxonomy_overview_terms_alter(&$form, &$form_state) {
  unset($form['actions']);
  $add_link = l('添加分类', 'admin/structure/taxonomy/'.$form['#vocabulary']->machine_name.'/add', array(
    'attributes' => array('class' => 'button'),
    'query' => drupal_get_destination(),
    ));
  $form['actions'] = array('#markup' => $add_link, '#type' => 'item');
}

function cc_admin_form_alter(&$form, $form_state, $form_id) {
}
